<template>
  <div class="app-container">
    <NormalForm pageTitle="操作日志" :operateForm="queryForm" :formList="formList" :enumOpt="enumOpt"
      @handleQuery="handleQuery" @handleReset="handleReset" />
    <div class="g-card" style="padding: 0 20px 20px 20px;">
      <div class="right-btn" style="display: flex; justify-content: space-between; margin-right: 0;">
        <div>
          <el-button type="info" plain icon="el-icon-download" @click="handleExport" v-hasPermi="['system:config:export']">导出</el-button>
        </div>
        <div>
          <el-button icon="el-icon-delete" :disabled="currentSelectList.length == 0" @click="handleDelete" v-hasPermi="['system:config:remove']">删除</el-button>
          <el-button type="primary" icon="el-icon-delete" @click="handleClean" v-hasPermi="['monitor:operlog:remove']">清空</el-button>
        </div>
      </div>
      <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="60" align="center" />
        <el-table-column label="日志编号" align="center" prop="operId" />
        <el-table-column label="系统模块" align="center" prop="title" />
        <el-table-column label="操作类型" align="center" prop="businessType">
          <template slot-scope="scope">
            <span>{{ typeOpt[scope.row.businessType] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="请求方式" align="center" prop="requestMethod" />
        <el-table-column label="操作人员" align="center" prop="operName" />
        <el-table-column label="主机" align="center" prop="operIp" :show-overflow-tooltip="true" />
        <el-table-column label="操作地点" align="center" prop="operLocation" :show-overflow-tooltip="true" />
        <el-table-column label="操作状态" align="center" prop="status">
          <template slot-scope="scope">
            <span>{{ stateOpt[scope.row.status] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作日期" align="center" prop="operTime" min-width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.operTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="120">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row, scope.index)" v-hasPermi="['monitor:operlog:query']">详细</el-button>
          </template>
        </el-table-column>
      </el-table>
      <NormalPagination :currentPage="queryForm.pageNum" :pageSize="queryForm.pageSize" :total="total" @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange"></NormalPagination>
    </div>
    <!-- 操作日志详细 -->
    <el-dialog title="操作日志详细" :visible.sync="detailVisible" width="700px" append-to-body :close-on-click-modal="false" :show-close="false">
      <el-form ref="detailForm" :model="detailForm" label-width="100px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作模块：">
              {{ detailForm.title }} / {{  typeOpt[detailForm.businessType] }}
            </el-form-item>
            <el-form-item label="登录信息：">
              {{ detailForm.operName }} / {{ detailForm.operIp }} / {{ detailForm.operLocation }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求地址：">{{ detailForm.operUrl }}</el-form-item>
            <el-form-item label="请求方式：">{{ detailForm.requestMethod }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作方法：">{{ detailForm.method }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求参数：">{{ detailForm.operParam }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="返回参数：">{{ detailForm.jsonResult }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作状态：">{{statusOpt[detailForm.status]}}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作时间：">{{ parseTime(detailForm.operTime) }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="异常信息：" v-if="detailForm.status === 1">{{ detailForm.errorMsg }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, delOperlog, cleanOperlog } from "@/api/monitor/operlog";
import { downLoadFile } from "@/utils/common";

export default {
  name: "Operlog",
  data() {
    return {
      queryForm: {
        title: '',
        operName: '',
        businessType: null,
        status: null,
        dateRange: [],
        pageNum: 1,
        pageSize: 20,
      },
      formList: [
        {
          label: '系统模块',
          value: '',
          prop: 'title',
          type: 'INPUT',
          emunName: ''
        },
        {
          label: '操作人员',
          value: '',
          prop: 'operName',
          type: 'INPUT',
          emunName: ''
        },
        {
          label: '操作类型',
          value: '',
          prop: 'businessType',
          type: 'SELECT',
          emunName: 'statusList'
        },
        {
          label: '操作状态',
          value: '',
          prop: 'status',
          type: 'SELECT',
          emunName: 'typeList'
        },
        {
          label: '创建',
          value: null,
          prop: 'dateRange',
          type: 'DATE_RANGE',
          emunName: '',
          formate: 'yyyy-MM-dd'
        },
      ],
      enumOpt: {
        statusList: [],
        typeList: [],
      },
      statusOpt: {},
      typeOpt: {},
      loading: true,
      total: 0,
      tableList: [],
      currentSelectList: [],
      detailVisible: false,
      detailForm: {},
    };
  },
  created() {
    this.handleQuery();
    this.getDicts("sys_oper_type").then((res) => {
      if (res.code == 200) {
        this.enumOpt.typeList = res.data;
        res.data.forEach(item => {
          this.typeOpt[item.dictValue] = item.dictLabel;
        })
      }
    });
    this.getDicts("sys_common_status").then((res) => {
      if (res.code == 200) {
        this.enumOpt.statusList = res.data;
        res.data.forEach(item => {
          this.statusOpt[item.dictValue] = item.dictLabel;
        })
      }
    });
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryForm.pageNum = 1;
      this.getTableListFn();
    },
    /** 重置按钮操作 */
    handleReset() {
      this.queryForm = {
        title: '',
        operName: '',
        businessType: null,
        status: null,
        dateRange: [],
        pageNum: 1,
        pageSize: 20,
      };
      this.getTableListFn();
    },
    handleSizeChange(val) {
      this.queryForm.pageNum = 1
      this.queryForm.pageSize = val
      this.getTableListFn()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum = val
      this.getTableListFn()
    },
    /** 查询登录日志 */
    getTableListFn() {
      this.loading = true;
      const params = {
        title: this.queryForm.title,
        operName: this.queryForm.operName,
        businessType: this.queryForm.businessType,
        status: this.queryForm.status,
        beginTime: this.queryForm.dateRange?.[0],
        endTime: this.queryForm.dateRange?.[1],
        pageNum: this.queryForm.pageNum,
        pageSize:  this.queryForm.pageSize,
      }
      list(params).then((res) => {
        this.tableList = res.rows || [];
        this.total = res.total || 0;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.currentSelectList = selection.map((item) => item.operId);
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.detailVisible = true;
      this.detailForm = row;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const operIds = row.operId || this.currentSelectList;
      this.$confirm('是否确认删除日志编号为"' + operIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        return delOperlog(operIds);
      }).then(() => {
        this.getTableListFn();
        this.$message.success("删除成功");
      })
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$confirm("是否确认清空所有操作日志数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        return cleanOperlog();
      }).then(() => {
        this.getTableListFn();
        this.$message.success("清空成功");
      }).catch(function () { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const params = {
        title: this.queryForm.title,
        operName: this.queryForm.operName,
        businessType: this.queryForm.businessType,
        status: this.queryForm.status,
        beginTime: this.queryForm.dateRange?.[0],
        endTime: this.queryForm.dateRange?.[1],
      }
      downLoadFile('/system/operlog/export', params, '操作日志.xlsx')
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../../styles/base.scss";
</style>
